package org.zakariya.stickyheadersapp;

import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.Image;
import ohos.agp.components.ListContainer;
import ohos.agp.components.Text;
import ohos.agp.utils.Color;
import org.zakariya.stickyheaders.StickyHeaderLayoutManager;
import org.zakariya.stickyheaders.Utils;
import org.zakariya.stickyheaders.model.HeaderModel;
import org.zakariya.stickyheaders.model.ItemModel;
import org.zakariya.stickyheadersapp.adapter.SectionAdapter;
import org.zakariya.stickyheadersapp.tool.TopToolUtils;

import java.util.List;
/**
 * SectionsAbility
 *
 * @since 2021-08-20
 */
public class SectionsAbility extends Ability {
    private SectionAdapter adapter;
    private Image iv_bottom;
    private Image iv_remove;
    private Image iv_menu;
    private ListContainer listContainer;
    private List<ItemModel> dataList;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_sections);
        getWindow().setStatusBarColor(Color.getIntColor("#303F9F"));
        initComponent();
    }

    private void initComponent() {
        Image image = (Image) findComponentById(ResourceTable.Id_icon_back);
        iv_bottom = (Image) findComponentById(ResourceTable.Id_iv_bottom);
        iv_remove = (Image) findComponentById(ResourceTable.Id_iv_remove);
        iv_menu = (Image) findComponentById(ResourceTable.Id_iv_menu);
        iv_bottom.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                TopToolUtils.showScrollTool(getContext(), listContainer);
            }
        });

        iv_remove.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                TopToolUtils.showViewToolNoFloat(getContext(), listContainer, dataList);
            }
        });

        iv_menu.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                TopToolUtils.showReloadTool(getContext(), listContainer);
            }
        });

        image.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                terminateAbility();
            }
        });
        Text headerText = (Text) findComponentById(ResourceTable.Id_sticky_text);
        Text indexText = (Text) findComponentById(ResourceTable.Id_index_text);
        listContainer = (ListContainer) findComponentById(ResourceTable.Id_list_sticky_callbacks);
        dataList = Utils.getStickyList(5, 5, true, HeaderModel.TYPE_70vp);
        adapter = new SectionAdapter(this, dataList);
        listContainer.setItemProvider(adapter);
        StickyHeaderLayoutManager headerDecor = new StickyHeaderLayoutManager(listContainer, null, headerText, indexText);
        headerDecor.setDataList(dataList,true);
        listContainer.setScrolledListener(new Component.ScrolledListener() {
            @Override
            public void onContentScrolled(Component component, int i, int i1, int i2, int i3) {

            }
        });
    }
}
